import { queryRole,createPostRole, createRole, updateRole, getAllMenusByRoleId, updateResource, jobsDetails} from '../services/api';
import { message } from 'antd';

export default {
  namespace: 'role',

  state: {
      roleList: [],
      jobsRoleList: [],
      page:[],
      resourcesList: [],
      loading: false,
  },

  effects: {
  
    *fetch({ payload }, { call, put }) {

      yield put({
        type: 'query',
        payload: {
          loading: true,
        },
      });
      const response = yield call(queryRole, payload);
      // console.log(response.data)
      yield put({
        type: 'query',
        payload: {
          roleList: response.data,
          // page: response.data.page,
          loading: false,
        },
      });
    },
    *roleSave({ payload , callback}, { call, put }) {

      yield put({
        type: 'query',
        payload: {
          loading: true,
        },
      });
      const response = yield call(createPostRole, payload);

      yield put({
        type: 'query',
        payload: {
          data: response
        },
      });
      if(callback)callback(response.result.code);
    },

    *createRole({ payload , callback}, { call, put }) {
      const response = yield call(createRole, payload);
      if(response.result.code == 200){
        message.success("添加成功")
      }else{
        message.error(response.result.message)
      }
      
      yield put({
        type: 'query',
        payload: {
          data: response
        },
      });
      if(callback)callback(response);
    },

    *updateRole({ payload , callback}, { call, put }) {
      // console.log(payload)
      const response = yield call(updateRole, payload);
      // console.log(response)
      if(response.result.code == 200){
        message.success("修改成功")
      }else{
        message.error(response.result.message)
      }
      yield put({
        type: 'query',
        payload: {
          data: response
        },
      });
      if(callback)callback(response);
    },

    *jobsDetails({ payload ,callback}, { call, put }) {
      const response = yield call(jobsDetails, payload);
      // console.log("jobsRoleList",response)
      yield put({
        type: 'query',
        payload: {
          jobsRoleList: response.data.roleList,
        },      
      });
      if(callback)callback()
    },

    *getAllMenusByRoleId({ payload , callback}, { call, put }) {
      const response = yield call(getAllMenusByRoleId, payload);
      console.log("response",response)
      yield put({
        type: 'query',
        payload: {
          resourcesList: response.data
        },
      });
      if(callback)callback(response);
    },

    *updateAllMenusByRoleId({ payload }, { call, put }) {
      // console.log("payload",payload)
      const response = yield call(updateResource, payload);
      // console.log("response",response)
      if(response.result.code == 200){
        message.success("权限配置成功")
      }else{
        message.error(response.result.message)
      }
      yield put({
        type: 'query',
        payload: {
          data: response.data
        },
      });
      // if(callback)callback(response);
    },
  },

  reducers: {
    query(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
  },
};
